﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Role_AddColumn2 : System.Web.UI.Page
{
    protected string Action = "";
    protected int ColumnID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断
        Action = NetRequest.GetQueryString("Action");
        ColumnID = NetRequest.GetQueryInt("ColumnID");
        if (!IsPostBack)
        {
            BindDrop();
            BindTreeDrop("");

            if (Action == "Modify" || Action == "Delete")
            {
                BindData();
            }
        }
    }

    protected void BindData()
    {
        Whir.Model.Whir_Column model = new Whir.SQLServerDAL.Whir_Column().GetModel(ColumnID);
        if (model == null)
            return;

        BindTreeDrop(" and columnID<>" + ColumnID);

        this.ddlModule.SelectedValue = model.moduleMark;
        if (model.moduleMark != "-1" && model.modelID != -1)
        {
            BindModel();
            this.ddlModel.SelectedValue = model.modelID.ToString();

            if (model.formID != "-1")
            {
                if (model.formID.IndexOf('|') != -1)    //表单组
                {
                    divForm.Visible = false;
                    divForms.Visible = true;
                    BindModelForms();
                    this.ddlModelForms.SelectedValue = model.formID;
                }
                else//表单
                {
                    divForms.Visible = false;
                    divForm.Visible = true;
                    BindModelForm();
                    this.ddlForm.SelectedValue = model.formID;
                }
            }
        }

        this.ddlDirectory.SelectedValue = model.directory;

        this.ddlParent.SelectedValue = model.parentID.ToString();
        this.txtColumnName.Text = model.columnName;
        this.txtOperatePage.Text = model.OperatePage;
        this.txtParam.Text = model.param;
        this.txtSort.Text = model.sort.ToString();
        this.txtUrl.Text = model.url;
        //图片 略
        this.txtMetaTitle.Text = model.metaTitle;
        this.txtMetaKey.Text = model.metaKey;
        this.txtMetaDesc.Text = model.metaDesc;

        this.txtListTemp.Text = model.listTemp;
        this.txtDefaultTemp.Text = model.defaultTemp;
        this.txtContentTemp.Text = model.contentTemp;

        this.cbIsCreateHtml.Checked = model.isCreateHtml;
        this.cbIsUse.Checked = model.isUse;

        this.cbIsUse.Enabled = false;    //权限不可改        
        this.rblIsSinglePage.SelectedIndex = model.IsSinglePage ? 0 : 1;
    }

    /// <summary>
    /// 绑定模块下拉列表
    /// </summary>
    protected void BindDrop()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("moduleMark,moduleName", "Whir_Module", "", "");
        this.ddlModule.DataSource = dt.DefaultView;
        this.ddlModule.DataValueField = "moduleMark";
        this.ddlModule.DataTextField = "moduleName";
        this.ddlModule.DataBind();

        this.ddlModule.Items.Insert(0, new ListItem("-- 模块列表 --", "-1"));
    }

    /// <summary>
    /// 绑定功能模块下拉列表
    /// </summary>
    protected void BindModel()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelID,modelName", "Whir_Model", "moduleMark='" + this.ddlModule.SelectedValue + "'", "");
        this.ddlModel.DataSource = dt.DefaultView;
        this.ddlModel.DataValueField = "modelID";
        this.ddlModel.DataTextField = "modelName";
        this.ddlModel.DataBind();

        this.ddlModel.Items.Insert(0, new ListItem("-- 功能模块列表 --", "-1"));
    }

    /// <summary>
    /// 绑定表单下拉列表
    /// </summary>
    protected void BindModelForm()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelFormID,formName", "Whir_ModelForm", "modelID=" + this.ddlModel.SelectedValue, "");
        this.ddlForm.DataSource = dt.DefaultView;
        this.ddlForm.DataValueField = "modelFormID";
        this.ddlForm.DataTextField = "formName";
        this.ddlForm.DataBind();
    }

    /// <summary>
    /// 绑定表单组下拉列表
    /// </summary>
    protected void BindModelForms()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("FormsGroup,GroupName", "Whir_ModelForms", "", "");
        this.ddlModelForms.DataSource = dt.DefaultView;
        this.ddlModelForms.DataValueField = "FormsGroup";
        this.ddlModelForms.DataTextField = "GroupName";
        this.ddlModelForms.DataBind();

        this.ddlModelForms.Items.Insert(0, new ListItem("-- 功能模块表单组 --", "-1"));
    }


    //列表事件1
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlModule.SelectedValue != "-1")
            BindModel();
        else
            this.ddlModel.Items.Clear();

        this.ddlForm.Items.Clear();
        this.ddlModelForms.Items.Clear();
    }

    //列表事件2
    protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlModel.SelectedValue != "-1")
        {
            Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
            bool flag = bll.GetModel(int.Parse(this.ddlModel.SelectedValue)).IsGroup;
            if (flag)
            {
                divForms.Visible = true;
                divForm.Visible = false;
                BindModelForms();
            }
            else
            {
                divForm.Visible = true;
                divForms.Visible = false;
                BindModelForm();
            }
        }
        else
        {
            this.ddlForm.Items.Clear();
            this.ddlModelForms.Items.Clear();
        }
    }

    /// <summary>
    /// 绑定栏目树型下拉列表
    /// </summary>    
    protected void BindTreeDrop(string str)
    {
        this.ddlParent.DataValueField = "columnID";
        this.ddlParent.DataTextField = "columnName";
        Whir.Common.BindHelper.BindDropDownList(this.ddlParent, "Whir_Column", "0", str, "-- 导航栏目 --");
    }


    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        switch (Action)
        {
            case "Add": AddColumn(); CloseDiv(); break;
            case "Modify": ModifyColumn(); CloseDiv(); break;
            case "Delete": DeleteColumn(); CloseDiv(); break;
        }
    }

    protected void CloseDiv()
    {
        Type type = typeof(Whir_System_Module_Role_AddColumn2);
        Page.ClientScript.RegisterStartupScript(type, "", "<script>closediv();</script>");
    }

    private void DeleteColumn()
    {
        Whir.SQLServerDAL.Whir_Column bll = new Whir.SQLServerDAL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(ColumnID);
        if (model != null)
        {
            bll.Delete(ColumnID);
        }

        //修改角色表之栏目id
        RoleHelper.UpdateColumnIDs(ColumnID.ToString());

        //修改角色表之角色值
        //if (this.cbIsUse.Checked)
        //{
        //    int b = Whir.SQLServerDAL.Whir_Column.GetIDSort(ColumnID);
        //    RoleHelper.DeleteColumnRole(b);
        //    RoleHelper.DeleteCacheRole(b);
        //}
    }

    private void ModifyColumn()
    {
        Whir.SQLServerDAL.Whir_Column bll = new Whir.SQLServerDAL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(ColumnID);

        model.moduleMark = this.ddlModule.SelectedValue;
        if (divForm.Visible)
        {
            if (this.ddlForm.Items.Count > 0)
                model.formID = this.ddlForm.SelectedValue;
            else
                model.formID = "-1";
        }
        else if (divForms.Visible)
        {
            if (this.ddlModelForms.Items.Count > 0)
                model.formID = this.ddlModelForms.SelectedValue;
            else
                model.formID = "-1";
        }

        if (this.ddlModel.Items.Count > 0)
            model.modelID = int.Parse(this.ddlModel.SelectedValue);
        else
            model.modelID = -1;

        model.parentID = int.Parse(this.ddlParent.SelectedValue);
        model.columnName = this.txtColumnName.Text;
        model.OperatePage = this.txtOperatePage.Text;
        model.param = this.txtParam.Text;
        model.sort = int.Parse(this.txtSort.Text);
        model.url = this.txtUrl.Text;
        model.pic = "";
        model.metaTitle = this.txtMetaTitle.Text;
        model.metaKey = this.txtMetaKey.Text;
        model.metaDesc = this.txtMetaDesc.Text;
        model.listTemp = this.txtListTemp.Text;
        model.defaultTemp = this.txtDefaultTemp.Text;
        model.contentTemp = this.txtContentTemp.Text;
        model.isCreateHtml = this.cbIsCreateHtml.Checked;

        model.updateDate = DateTime.Now;
        model.updateUserID = RoleHelper.GetAdminID();
        model.directory = this.ddlDirectory.SelectedValue;
        model.IsSinglePage = rblIsSinglePage.SelectedIndex == 0 ? true : false;
        bll.Update(model);
    }

    private void AddColumn()
    {
        string form = this.ddlForm.SelectedValue;
        Whir.Model.Whir_Column model = new Whir.Model.Whir_Column();

        model.columnMark = "Whir_" + DateTime.Now.ToString();
        model.moduleMark = this.ddlModule.SelectedValue;

        if (this.ddlModel.Items.Count > 0)
            model.modelID = int.Parse(this.ddlModel.SelectedValue);
        else
            model.modelID = -1;

        if (divForm.Visible)
        {
            if (this.ddlForm.Items.Count > 0)
                model.formID = this.ddlForm.SelectedValue;
            else
                model.formID = "-1";
        }
        else if (divForms.Visible)
        {
            if (this.ddlModelForms.Items.Count > 0)
                model.formID = this.ddlModelForms.SelectedValue;
            else
                model.formID = "-1";
        }
        else
            model.formID = "-1";

        model.parentID = int.Parse(this.ddlParent.SelectedValue);
        model.columnName = this.txtColumnName.Text;
        model.OperatePage = this.txtOperatePage.Text;
        model.param = this.txtParam.Text;
        model.sort = int.Parse(this.txtSort.Text);
        model.url = this.txtUrl.Text;
        model.pic = "";
        model.metaTitle = this.txtMetaTitle.Text;
        model.metaKey = this.txtMetaKey.Text;
        model.metaDesc = this.txtMetaDesc.Text;
        model.listTemp = this.txtListTemp.Text;
        model.defaultTemp = this.txtDefaultTemp.Text;
        model.contentTemp = this.txtContentTemp.Text;
        model.isCreateHtml = this.cbIsCreateHtml.Checked;
        model.isUse = this.cbIsUse.Checked;

        model.createDate = DateTime.Now;
        model.createUserID = RoleHelper.GetAdminID();
        model.updateDate = model.createDate;
        model.updateUserID = model.createUserID;
        model.directory = this.ddlDirectory.SelectedValue; 
        model.IsSinglePage = rblIsSinglePage.SelectedIndex == 0 ? true : false;
        new Whir.SQLServerDAL.Whir_Column().Add(model);

        //同步权限
        if (this.cbIsUse.Checked)
        {
            RoleHelper.AddDefaultRole();
            RoleHelper.AddCacheRole();
        }
        //end
    }
}
